package cn.iocoder.yudao.module.system.dal.mysql.application;

import java.util.*;


import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.application.vo.ApplicationPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.application.ApplicationDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;


/**
 * 目录审批 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ApplicationMapper extends BaseMapperX<ApplicationDO> {

    default PageResult<ApplicationDO> selectPage(ApplicationPageReqVO reqVO) {
        LambdaQueryWrapperX<ApplicationDO> queryWrapper = new LambdaQueryWrapperX<ApplicationDO>()
                .eqIfPresent(ApplicationDO::getCatalogId, reqVO.getCatalogId())
                .eqIfPresent(ApplicationDO::getApplicationType, reqVO.getApplicationType())
                .likeIfPresent(ApplicationDO::getApplicationTitle, reqVO.getApplicationTitle())
                .eqIfPresent(ApplicationDO::getApplicationReason, reqVO.getApplicationReason())
                .eqIfPresent(ApplicationDO::getApplicationScenario, reqVO.getApplicationScenario())
                .eqIfPresent(ApplicationDO::getUseTimeStart, reqVO.getUseTimeStart())
                .eqIfPresent(ApplicationDO::getUseTimeEnd, reqVO.getUseTimeEnd())
                .eqIfPresent(ApplicationDO::getContactPhone, reqVO.getContactPhone())
                .eqIfPresent(ApplicationDO::getContactAddress, reqVO.getContactAddress())
                .eqIfPresent(ApplicationDO::getServiceScope, reqVO.getServiceScope())
                .eqIfPresent(ApplicationDO::getExpectedEffect, reqVO.getExpectedEffect())
                .likeIfPresent(ApplicationDO::getSystemName, reqVO.getSystemName())
                .eqIfPresent(ApplicationDO::getDeploymentLocation, reqVO.getDeploymentLocation())
                .eqIfPresent(ApplicationDO::getSystemIp, reqVO.getSystemIp())
                .eqIfPresent(ApplicationDO::getSystemUrl, reqVO.getSystemUrl())
                .eqIfPresent(ApplicationDO::getLevelProtectionCode, reqVO.getLevelProtectionCode())
                .eqIfPresent(ApplicationDO::getLevelProtectionLevel, reqVO.getLevelProtectionLevel())
                .eqIfPresent(ApplicationDO::getCommitmentFile, reqVO.getCommitmentFile())
                .eqIfPresent(ApplicationDO::getCallCountLimit, reqVO.getCallCountLimit())
                .eqIfPresent(ApplicationDO::getCallIntervalSeconds, reqVO.getCallIntervalSeconds())
                .eqIfPresent(ApplicationDO::getTargetDatabaseAddress, reqVO.getTargetDatabaseAddress())
                .likeIfPresent(ApplicationDO::getTargetDatabaseTableName, reqVO.getTargetDatabaseTableName())
                .eqIfPresent(ApplicationDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ApplicationDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(ApplicationDO::getCreateBy, reqVO.getCreateBy())
                .orderByDesc(ApplicationDO::getId);

        // 如果 createBy 为空，则排除 status = 0 的数据
        if (reqVO.getCreateBy() == null) {
            queryWrapper.ne(ApplicationDO::getStatus, 0);
        }

        return selectPage(reqVO, queryWrapper);
    }

    List<ApplicationDO> getApplicationAuditList(@Param("pageReqVO") ApplicationPageReqVO pageReqVO);

    Long getApplicationAuditPageCount(@Param("pageReqVO")ApplicationPageReqVO pageReqVO);

    void updateStatus(@Param("id") Integer applicationId, @Param("status") String status);

    ApplicationDO getApplicationDetail(@Param("pageReqVO") ApplicationPageReqVO pageReqVO);

    Integer getAuditCount(@Param("auditor") String  auditor);
}